package com.aopcinemas.visitor.ui;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.List;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import com.aopcinemas.cinema.model.Schedule;
import com.aopcinemas.visitor.VisitorAgent;
import com.aopcinemas.visitor.VisitorUtils;
import com.aopcinemas.visitor.event.ScheduleInfoListener;
import com.aopcinemas.visitor.ontology.CampaignRequest;
import com.aopcinemas.visitor.ontology.GetCampaign;

public class VisitorFrame extends JFrame implements ScheduleInfoListener {

    private static final long serialVersionUID = 1L;

    private VisitorAgent agent;

    private MovieSearchAgentDialog searchAgentDialog;
    private CampaignCreationDialog campaignCreationDialog;
    private CampaingSearchAgentDialog campaingSearchAgentDialog;
    private CampaignRequestDialog campaignRequestDialog;

    public VisitorFrame(VisitorAgent agent) {
        this.agent = agent;
        initializeUI();
        initializeMenuBar();
        initializeFrame();
    }

    private void initializeUI() {
        searchAgentDialog = new MovieSearchAgentDialog(this, agent);
        campaignCreationDialog = new CampaignCreationDialog(this, agent);
        campaingSearchAgentDialog = new CampaingSearchAgentDialog(this, agent);
        campaignRequestDialog = new CampaignRequestDialog(this, agent);
        initializePanel();
    }

    private void initializePanel() {

        try {
            JPanel panel = new JPanel();

            // Movie search agent
            ImageIcon icon = new ImageIcon(ImageIO.read(ClassLoader.getSystemResourceAsStream("schedule_search.png")));
            JButton button = new JButton(icon);
            button.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent evt) {
                    searchAgentDialog.setLocationRelativeTo(VisitorFrame.this);
                    searchAgentDialog.setVisible(true);
                }
            });
            panel.add(button);

            // Group visit agent
            icon = new ImageIcon(ImageIO.read(ClassLoader.getSystemResourceAsStream("group_visit_search.png")));
            button = new JButton(icon);
            button.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent evt) {
                    campaingSearchAgentDialog.setLocationRelativeTo(VisitorFrame.this);
                    campaingSearchAgentDialog.setVisible(true);
                }
            });

            panel.add(button);
            getContentPane().add(panel);

        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }

    private void initializeFrame() {
        setTitle("Visitor");
        setSize(new Dimension(400, 250));
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    }

    private void initializeMenuBar() {
        JMenuBar menuBar = new JMenuBar();
        JMenu file = new JMenu("File");
        menuBar.add(file);

        JMenuItem addAgent = new JMenuItem("Add movie search");
        addAgent.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent evt) {
                searchAgentDialog.setLocationRelativeTo(VisitorFrame.this);
                searchAgentDialog.setVisible(true);
            }
        });
        file.add(addAgent);

        JMenuItem addCampaignAgent = new JMenuItem("Add campaign search");
        addCampaignAgent.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                campaingSearchAgentDialog.setLocationRelativeTo(VisitorFrame.this);
                campaingSearchAgentDialog.setVisible(true);
            }
        });
        file.add(addCampaignAgent);

        JMenuItem exit = new JMenuItem("Exit");
        exit.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent evt) {
                VisitorFrame.this.setVisible(false);
                VisitorUtils.unregisterCampaign(VisitorFrame.this.agent);
                VisitorFrame.this.agent.doDelete();
            }
        });
        file.add(exit);

        setJMenuBar(menuBar);
    }

    @Override
    public void listReceived(List<Schedule> schedules) {
        if (!campaignCreationDialog.isVisible()) {
            campaignCreationDialog.setSchedules(schedules);
            campaignCreationDialog.setLocationRelativeTo(VisitorFrame.this);
            campaignCreationDialog.setVisible(true);
        }
    }

    public void campaignReceived(GetCampaign getCampaign) {
        if (!campaignRequestDialog.isVisible()) {
            campaignRequestDialog.reinit(getCampaign);
            campaignRequestDialog.setVisible(true);
        }
    }

    public boolean campaignRequestReceived(CampaignRequest campaignRequest) {
        JPanel panel = new JPanel();
        panel.add(new JLabel("uid: " + campaignRequest.getPerson().getUid()));
        panel.add(new JLabel("name: " + campaignRequest.getPerson().getName()));
        return JOptionPane.showConfirmDialog(this, panel, "Confirm campaign request", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION;
    }

    public void notifyMessage(String content) {
        JOptionPane.showMessageDialog(this, content);
    }
}
